<!--
* description: 国地补资料上传
* author: wangjuan
* createdDate: 2020-11-18
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
    />

    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible.sync="curPopupsVisible"
      :popups-state="editPopupsState"
      @closeEdit="closeEdit"
    />

    <lookData
      :key="lookPopupsKey"
      :dynamic-edit-row-data="lookRowData"
      :popups-visible="lookPopupsVisible"
      :popups-state="editPopupsState"
      @closelookData="closelookData"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import lookData from './lookData'
import api from '@/api/subsidyManage'

export default {
  name: 'StateLandSubsidyDataUpload',

  components: {
    OneTableTemplate,
    Edit: () => import('./edit'),
    lookData
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      lookPopupsKey: 'look',
      lookPopupsState: '',
      lookPopupsVisible: false,
      lookRowData: {},

      editPopupsKey: 'editPopupsKey',
      editRowData: {},
      curPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: api.queryCountryUpload,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '订单编号：',
              codeField: 'subSaleOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '订单标签：',
              codeField: 'saleOrderSign',
              lookuptype: 'VE1013',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '购买方式：',
              codeField: 'buyTypeId',
              lookuptype: 'VE0117',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '联系人姓名：',
              codeField: 'custLxName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '联系人电话：',
              codeField: 'custLxPhone',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '车牌号码：',
              codeField: 'carLicenseNo',
              validrule: { noRequire: true, iscPattern: true },
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey8',
              labelName: '审核状态：',
              codeField: 'colStatus',
              lookuptype: 'VE1086',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'subsidyId',
              label: 'id',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn',
              label: '操作',
              fixed: true,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '录入',
                  codeField: 'add',
                  rowFileds: 'show:colStatus-未录入',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.edit(index)
                },
                {
                  compKey: 'propKey2',
                  labelName: '查看',
                  codeField: 'look',
                  rowFileds: 'show:colStatus-已申请|区域审核通过|总部审核通过',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.lookData(index)
                },
                {
                  compKey: 'propKey3',
                  labelName: '编辑',
                  codeField: 'edit',
                  rowFileds: 'show:colStatus-已录入|区域驳回|总部驳回',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.edit(index)
                },
                {
                  compKey: 'propKey4',
                  labelName: '申请',
                  rowFileds: 'show:colStatus-已录入|区域驳回|总部驳回',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.aduit(index)
                }
              ]
            },
            {
              prop: 'downloadAttachment',
              label: '下载附件',
              width: 100,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey5',
                  codeField: 'downloadAttachment',
                  rowFileds: 'show:colStatus-已申请|区域审核通过|总部审核通过|已录入|区域驳回|总部驳回',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.download(index),
                  labelName: '下载附件'
                },
                {
                  compKey: 'propKey6',
                  codeField: 'nodown',
                  rowFileds: 'show:colStatus-未录入',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: () => null,
                  labelName: '当前状态无附件'
                }
              ]
            },
            {
              prop: 'colStatus',
              label: '审核状态'
            },
            {
              prop: 'subSaleOrderCode',
              label: '订单编号',
              width: 100
            },
            {
              prop: 'saleOrderSignCode',
              label: '订单标签'
            },
            {
              prop: 'buyTypeId',
              label: '购买方式'
            },
            {
              prop: 'custLxName',
              label: '联系人姓名'
            },
            {
              prop: 'custLxPhone',
              label: '联系人电话',
              width: 100
            },
            {
              prop: 'qualifiedCertNo',
              label: '合格证编号'
            },
            {
              prop: 'produceDate',
              label: '车辆制造日期',
              width: 140
            },
            {
              prop: 'carConfigCn',
              label: '车型名称'
            },
            {
              prop: 'provinceName',
              label: '补贴上牌省份'
            },
            {
              prop: 'cityName',
              label: '补贴上牌城市'
            },
            {
              prop: 'invoiceNo',
              label: '发票号'
            },
            {
              prop: 'invoiceDate',
              label: '开票日期',
              width: 140
            },
            {
              prop: 'vin',
              label: 'VIN码',
              width: 140
            },
            {
              prop: 'invoicePrice',
              label: '开票价格'
            },
            {
              prop: 'dlrFullName',
              label: '销货单位名称',
              width: 140
            },
            {
              prop: 'licenseDate',
              label: '行驶证注册日期',
              width: 140
            },
            {
              prop: 'carLicenseNo',
              label: '车牌号码'
            },
            {
              prop: 'licenseBelong',
              label: '行驶证所有人'
            },
            {
              prop: 'bandCode',
              label: '品牌型号'
            },
            {
              prop: 'carUsePropCode',
              label: '使用性质'
            },
            {
              prop: 'custTypeCode',
              label: '开票人类型'
            },
            {
              prop: 'enterpriseName',
              label: '企业名称'
            },
            {
              prop: 'socialCode',
              label: '统一社会信用代号',
              width: 140
            },
            {
              prop: 'enterprisePhone',
              label: '企业联系电话',
              width: 100
            },
            {
              prop: 'enterpriseAdd',
              label: '企业地址'
            },
            {
              prop: 'corpName',
              label: '法人姓名'
            },
            {
              prop: 'corpCredNo',
              label: '法人身份证号',
              width: 140
            },
            {
              prop: 'corpRepPhone',
              label: '法人联系电话',
              width: 100
            },
            {
              prop: 'corpAddress',
              label: '法人地址',
              width: 140
            },
            {
              prop: 'writeMen',
              label: '录入人'
            },
            {
              prop: 'writeTime',
              label: '录入日期',
              width: 140
            },
            {
              prop: 'modifier',
              label: '最后修改人'
            },
            {
              prop: 'lastUpdatedDate',
              label: '最后修改时间',
              width: 140
            },
            { prop: 'applyMen',
              label: '申请人'
            },
            {
              prop: 'applyTime',
              label: '申请时间',
              width: 140
            }
          ],

      // 表单数据
      formField: this.$utils.getFormField(this, {
        subSaleOrderCode: '',
        vin: '',
        saleOrderSign: '',
        buyTypeId: '',
        custLxName: '',
        custLxPhone: '',
        carLicenseNo: '',
        colStatus: '',
        updateControlId: '',
        subsidyId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 下载附件
    download(index) {
      var datalist = this.$refs.multipleTable.list[index]
      var data
      data = [
        datalist.corpFile,
        datalist.invoiceFile,
        datalist.enterpriseFile ? datalist.enterpriseFile : '',
        datalist.drlFile ? datalist.drlFile : ''
      ]
      this.$utils.FileDownload(data)
    },
    // 申请
    aduit(index) {
      this.$confirm('是否确定要进行申请', '提示', {
        type: 'warning'
      }).then(() => {
        this.formField.updateControlId = this.$refs['multipleTable'].list[index].updateControlId
        this.formField.subsidyId = this.$refs['multipleTable'].list[index].subsidyId
        const queryObj = {
        // 保存mutation
          type: 'mutation',
          // api配置
          apiConfig: api.applyCountryUpload,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            updateControlId: this.formField.updateControlId,
            subsidyId: this.formField.subsidyId
          }
        }

        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        // 调用中台API方法（可复用）
        this.$requestAPI(paramD).then(response => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
            this.$nextTick(() => {
              this.$refs.multipleTable.queryTable(1)
            })
          } else {
          // 申请失败
            this.$message({
              message: response.msg
            })
          }
        }).catch(err => {
          console.log(err)
        })
      })
    },

    // 资料查看弹窗
    lookData(index) {
      this.lookPopupsVisible = true
      this.editPopupsState = 'edit'
      this.lookRowData = this.$refs['multipleTable'].list[index]
    },

    // 关闭资料查看弹窗
    closelookData() {
      this.lookPopupsVisible = false
      this.lookPopupsKey = this.$utils.generateId()
    },

    edit(index) {
      this.editPopupsState = 'edit'
      this.curPopupsVisible = true
      this.editRowData = this.$refs['multipleTable'].list[index]
    },

    closeEdit() {
      this.curPopupsVisible = false
      this.editPopupsKey = this.$utils.generateId()
    }
  }
}
</script>
